//
//  FrdRqstMsgDetailViewController.m
//  SIPphone
//
//  Created by wrt on 13-11-28.
//  Copyright (c) 2013年 wrtsoft. All rights reserved.
//

#import "FrdRqstMsgDetailViewController.h"
#import "FriendRespondDateBase.h"
#import "ContactsDateBase.h"
#import "SIPInterface.h"

@interface FrdRqstMsgDetailViewController ()
{
    NSString *tempUserName;
    NSString *tempDisplayName;
    NSString *tempIpPort;
    NSString *tempisDo;
}

@end

@implementation FrdRqstMsgDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"好友请求信息";
    }
    return self;
}

- (id)initWithUserName:(NSString *)userName andDisplayName:(NSString *)displayName andipPort:(NSString *)ip andIsDo:(NSString *)isDo
{
    self = [super init];
    if (self)
    {
        tempUserName = [[NSString alloc] init];
        tempDisplayName = [[NSString alloc] init];
        tempIpPort = [[NSString alloc] init];
        tempisDo = [[NSString alloc] init];
        tempUserName = userName;
        tempDisplayName = displayName;
        tempIpPort = ip;
        tempisDo = isDo;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if ([tempisDo isEqualToString:@"未处理"])
    {
        [self.isAlreallyDo setHidden:YES];
    }
    else if ([tempisDo isEqualToString:@"已处理"])
    {
        [self.acceptButton setHidden:YES];
        [self.rejectButton setHidden:YES];
    }
    self.usernameLabel.text = tempUserName;
    if ([tempDisplayName isEqualToString:@"(null)"])
    {
        self.displayNameLabel.text = tempUserName;
    }
    else
    {
        self.displayNameLabel.text = tempDisplayName;
    }

    self.ipportNameLabel.text = tempIpPort;
}

- (IBAction)acceptButtonPressed:(id)sender
{
    //修改为已操作
    FriendRespondDateBase *friendRspdDB = [FriendRespondDateBase shareFriendRespondDateBase];
    [friendRspdDB updateDBForIsDoWithUserName:tempUserName andIP:tempIpPort];
    
//    //将接受的指令传递下去
//    SIPInterface *interface = [SIPInterface shareSIPInterface];
//    [interface acceptAddFriendWithUserName:tempUserName andDisPlayName:tempDisplayName andIp:tempIpPort];
//    BOOL isExistContacts = NO;
    //将数据加入到联系人里面
    ContactsDateBase *contactsDB = [ContactsDateBase shareContactsDataBase];
//    for (int i = 0; i < contactsDB.userNameArray.count; i++)
//    {
//        if ([contactsDB.userNameArray[i] isEqualToString:tempUserName] && [contactsDB.ipPortArray[i] isEqualToString:tempIpPort])
//        {
//            isExistContacts = YES;
//            break;
//        }
//    }
    
//    if (isExistContacts == NO)
//    {
        if (self.markNameTextField.text.length > 0)
        {
//            [contactsDB saveAcceptFriendIntoDBWithUserName:tempUserName andMarkName:self.markNameTextField.text andIp:tempIpPort];
            [contactsDB saveFriendIntoDBWithUserName:tempUserName andMarkName:self.markNameTextField.text andIp:tempIpPort andDisplayName:tempDisplayName];
        }
        else
        {
//            [contactsDB saveAcceptFriendIntoDBWithUserName:tempUserName andMarkName:@"" andIp:tempIpPort];
            [contactsDB saveFriendIntoDBWithUserName:tempUserName andMarkName:tempDisplayName andIp:tempIpPort andDisplayName:tempDisplayName];
        }
//    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:@"添加成功" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alert show];
    [self.navigationController popViewControllerAnimated:YES];
    [alert dismissWithClickedButtonIndex:0 animated:YES];
}

- (IBAction)rejectButtonPressed:(id)sender
{
    //修改为已操作
    FriendRespondDateBase *friendRspdDB = [[FriendRespondDateBase alloc ] init];
    [friendRspdDB updateDBForIsDoWithUserName:tempUserName andIP:tempIpPort];
    
//    //将拒绝的指令传递下去
//    SIPInterface *interface = [SIPInterface shareSIPInterface];
//    [interface rejectAddFriendWithUserName:tempUserName andDisPlayName:tempDisplayName andIp:tempIpPort];
//    [interface deleteFriendWithUserName:tempUserName andDisPlayName:tempDisplayName andIp:tempIpPort];
    
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:@"已拒绝该请求" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alert show];
    [self.navigationController popViewControllerAnimated:YES];
    [alert dismissWithClickedButtonIndex:0 animated:YES];
}

- (IBAction)markNameTextfieldDidEnd:(id)sender
{
    [self.markNameTextField resignFirstResponder];
}

- (IBAction)backgroundTouch:(id)sender
{
    [self.markNameTextField resignFirstResponder];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
